package com.baihan.angus.app.game.busshow.pop_dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.baihan.angus.app.game.basic.utils.AppUtil;
import com.baihan.angus.app.game.basic.utils.ComUtil;
import com.baihan.angus.app.game.busshow.R;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

public class AppDialog extends Dialog {

    private Callback mCallback;
    private Context mContext;
    private WindowManager.LayoutParams mLayoutParams;
    private TextView tvMessage, tvDetermine, tvCancel;
    private ImageView ivCancel, ivDetermine;
    private String message, cancel, determine;
    private Drawable daCancel, daDetermine;
    private LinearLayout llCancel, llDetermine;

    public AppDialog(@NonNull Context context, Callback mCallback) {
        super(context, R.style.FullDialog);
        this.mContext = context;
        this.mCallback = mCallback;
        initContentView();
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        mLayoutParams.height = ComUtil.Dp2Px(context, 175);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(120, 0, 120, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.CENTER);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_app_dialog, (ViewGroup) null);
        setContentView(dialogView);
        tvMessage = dialogView.findViewById(R.id.tvMessage);
        tvDetermine = dialogView.findViewById(R.id.tvDetermine);
        tvCancel = dialogView.findViewById(R.id.tvCancel);
        ivCancel = dialogView.findViewById(R.id.ivCancel);
        ivDetermine = dialogView.findViewById(R.id.ivDetermine);
        llCancel = dialogView.findViewById(R.id.llCancel);
        llDetermine = dialogView.findViewById(R.id.llDetermine);

        setMessage(message);
        setDetermine(determine);
        setCancel(cancel);
        setCancelPic(daCancel);
        setDeterminePic(daDetermine);

        llCancel.setOnClickListener(v -> cancel());

        llDetermine.setOnClickListener(v -> {
            if (null != mCallback) {
                mCallback.onDetermine();
            }
        });
    }

    public void setMessage(String message) {
        this.message = message;
        if (TextUtils.isEmpty(message)) {
            tvMessage.setVisibility(GONE);
        } else {
            tvMessage.setVisibility(VISIBLE);
            tvMessage.setText(message);
        }
    }

    public void setDetermine(String determine) {
        this.determine = determine;
        if (TextUtils.isEmpty(determine)) {
            tvDetermine.setVisibility(GONE);
        } else {
            tvDetermine.setVisibility(VISIBLE);
            tvDetermine.setText(determine);
        }
    }

    public void setDetermineColor(int color) {
        tvDetermine.setTextColor(color);
    }

    public void setCancel(String cancel) {
        this.cancel = cancel;
        if (TextUtils.isEmpty(cancel)) {
            tvCancel.setVisibility(GONE);
            llCancel.setVisibility(GONE);
        } else {
            llCancel.setVisibility(VISIBLE);
            tvCancel.setVisibility(VISIBLE);
            tvCancel.setText(cancel);
        }
    }

    public void setCancelPic(Drawable daCancel) {
        this.daCancel = daCancel;
        if (daCancel == null) {
            ivCancel.setVisibility(GONE);
        } else {
            ivCancel.setVisibility(VISIBLE);
            ivCancel.setImageDrawable(daCancel);
        }
    }

    public void setDeterminePic(Drawable daDetermine) {
        this.daDetermine = daDetermine;
        if (daDetermine == null) {
            ivDetermine.setVisibility(GONE);
        } else {
            ivDetermine.setVisibility(VISIBLE);
            ivDetermine.setImageDrawable(daDetermine);
        }
    }

    public interface Callback {
        void onDetermine();
    }
}
